﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using WorldOfArtWebApp.DataModels;
using ArtWorldObjects;

namespace WorldOfArtWebApp.DataAccess
{
    public class WorksOfArtData : IWorksOfArtData
    {
        protected WorldOfArtContext m_context;
        protected ArtPiece m_workOfArt;
        protected List<ArtPiece> m_workOfArtList;
        protected Artist m_artist;
        protected string m_artistName;
        protected string m_exceptionMessage;

        public WorksOfArtData()
        {
            m_artist = new Artist();
            m_context = new WorldOfArtContext();
            m_workOfArtList = new List<ArtPiece>();
            m_exceptionMessage = string.Empty;
            GetWorksOfArt();
        }

        public WorksOfArtData(Artist artist)
        {
            m_artist = artist;
            m_context = new WorldOfArtContext();
            m_workOfArtList = new List<ArtPiece>();
            m_exceptionMessage = string.Empty;
            GetWorksOfArtByArtist();
        }

        public ObjectContext WorksOfArtContext
        {
            get { return m_context; }
        }

        public ArtPiece WorkOfArt
        {
            get { return m_workOfArt; }
        }

        public List<ArtPiece> WorkOfArtList
        {
            get { return m_workOfArtList; }
        }

        public Artist Artist
        {
            get { return m_artist; }
        }

      
        public string ExceptionMessage
        {
            get { return m_exceptionMessage; }
        }

        public void GetWorksOfArt()
        {

            try
            {
                var workOfArtQuery = from wa in m_context.aspnet_WorksOfArt
                                        select wa;
                foreach (var workOfArt in workOfArtQuery)
                {
                    m_workOfArt = new ArtPiece();
                    m_workOfArt.Title = workOfArt.Title;
                    m_workOfArt.TheArtist = new Artist();
                    m_workOfArt.TheArtist.Name = workOfArt.aspnet_Artist.FirstName + " " + workOfArt.aspnet_Artist.LastName;
                   
                    m_workOfArtList.Add(m_workOfArt);
                }
            }
            catch (Exception ex)
            {
                m_exceptionMessage = ex.Message;
            }
        }

        public void GetWorksOfArtByArtist()
        {
            try
            {
                var workOfArtQuery = from wa in m_context.aspnet_WorksOfArt
                                     where Artist.ID == wa.ArtistID
                                     select wa;
                foreach (var workOfArt in workOfArtQuery)
                {
                    m_workOfArt = new ArtPiece();
                    m_workOfArt.Title = workOfArt.Title;

                }
            }
            catch (Exception ex)
            {
                m_exceptionMessage = ex.Message;
            }
        }
    }
}